package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.User;
import com.itheima.reggie.exception.BusinessException;
import com.itheima.reggie.mapper.UserMapper;
import com.itheima.reggie.service.UserService;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    /**
     * 用户登录的方法
     *
     * @param phone       手机号
     * @param userCode    用户输入的验证码
     * @param sessionCode session
     * @return
     */
    @Override
    public User login(String phone, String userCode, String sessionCode) {
        if (!userCode.equals(sessionCode)) {
            throw new BusinessException("验证码错误");
        }

        User loginUser = userMapper.findByPhone(phone);
        if(loginUser!=null){
            if (loginUser.getStatus() == 1) {
                throw new BusinessException("你的手机号已被禁用");
            }
        }else{
            loginUser = new User();
            loginUser.setStatus(0);
            loginUser.setPhone(phone);
            userMapper.save(loginUser);
        }
        return loginUser;
    }
}
